package edu.utn.tacs.grupouno.ui.client;

import java.util.List;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CellPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionModel.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionModel.SelectionChangeHandler;
import com.google.gwt.view.client.SingleSelectionModel;

import edu.utn.tacs.grupouno.ui.client.dto.PacienteDTO;

public class PacientesPanel extends VerticalPanel {

	public PacientesPanel() {
		initComponents();
	}

	private void initComponents() {
		final Panel me = this;
		add(new Label("Cargando..."));
		PacientesUIServiceAsync service = GWT.create(PacientesUIService.class);
		service.getPacientes(new AsyncCallback<List<PacienteDTO>>() {

			@Override
			public void onSuccess(List<PacienteDTO> pacientes) {
				me.clear();

				// Create a CellList that uses the cell
				CellList<PacienteDTO> cellList = new CellList<PacienteDTO>(new PacienteCell());

				// Set the total row count. This isn't strictly necessary, but it affects
				// paging calculations, so its good habit to keep the row count up to date.
				cellList.setDataSize(pacientes.size(), true);
				cellList.setData(0, pacientes.size(), pacientes);

				// Add a selection model using the same keyProvider.
				final SingleSelectionModel<PacienteDTO> selectionModel = new SingleSelectionModel<PacienteDTO>();
				cellList.setSelectionModel(selectionModel);

				selectionModel.addSelectionChangeHandler(new SelectionChangeHandler() {
					
					@Override
					public void onSelectionChange(SelectionChangeEvent event) {
						PacienteDTO selectedPaciente = selectionModel.getSelectedObject();
						Panel panel = Application.getContentPanel();
						panel.clear();
						panel.add(new AnalisisPanel(selectedPaciente.getId()));
					}
				});

				Label title = new Label("Pacientes");
				title.setStyleName("title");
				me.add(title);
				//DecoratorPanel decoratorPanel = new DecoratorPanel();
				ScrollPanel scPanel = new ScrollPanel();
				//scPanel.setAlwaysShowScrollBars(true);
				InnerPacientesPanel p = new InnerPacientesPanel();
				p.add(scPanel);
				me.add(p);
				p.add(cellList);

			}

			@Override
			public void onFailure(Throwable arg0) {
				me.clear();
				me.add(new ErrorLabel("Error: " + arg0.getMessage()));
				arg0.printStackTrace();
			}
		});

	}


	private static class PacienteCell extends AbstractCell<PacienteDTO> {

		public PacienteCell() {

		}

		@Override
		public void render(PacienteDTO pacienteDTO, Object key, StringBuilder sb) {
			if (pacienteDTO != null) {
				sb.append(pacienteDTO.getNombre());
			}
		}

	}
	
	private class InnerPacientesPanel extends CellPanel {
	    public void add(Widget child) {
	        Element tr = DOM.createTR();
	        Element td = DOM.createTD();
	        DOM.appendChild(tr, td);
	        DOM.appendChild(getBody(), tr);
	        super.add(child, td);
	    }
	}

}
